package com.wonders.frame.common.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * @author liuxu
 * @create 2020-11-02 10:54
 */
public class RSAUtil {

    private static final String PUB_Key="" +
            "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALj8XTFbY5QRCSh8no9+rIdAWJtuKQCE\n" +
            "RYmqExdlzA8V8ZmD1B0GktWLZjT3TvRZTjJfdEw8xkvoWltO4/i0RXUCAwEAAQ==";

    private static final String PRI_KEY="" +
            "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAuPxdMVtjlBEJKHye\n" +
            "j36sh0BYm24pAIRFiaoTF2XMDxXxmYPUHQaS1YtmNPdO9FlOMl90TDzGS+haW07j\n" +
            "+LRFdQIDAQABAkEAqEjM7u3YtQaacj6McLYodhJPiXa5zkKGkCosnAh7juw2qPv8\n" +
            "H325IGXmg2q/ULSOJdumBrSPGHcMYTPKoe+zsQIhAOlhegmjWU2A++rBKae5YC00\n" +
            "cBhY+zHQyJRd0rGfYOyDAiEAyuoi4SYt1/KuikTkS8GcrapnMVhSk3IvBYVkyYl1\n" +
            "VKcCIQCVduwliAwFavnrmtIGrN4/lTnFHheiC8ip3Hy3bQ8x6QIgZmQLQa1wSJ3O\n" +
            "8TqeMJP8Ka3o8H9u+7QMgi6zkvbn29UCIEXdM1ZtlraRKGy1R4uwHmrdPeSFiq9r\n" +
            "MoGNaF1jtRYw";

    /**
     * RSA公钥加密
     * @param info 待加密字符串
     * @return 加密后的密文
     */
    public  static String entryInfo(String info ) throws Exception{
        String entryStr = "";
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(PUB_Key);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        entryStr = Base64.encodeBase64String(cipher.doFinal(info.getBytes("UTF-8")));
        return entryStr;
    }

    /**
     * RSA 私钥解密
     * @param entryStr 密文
     * @return 解密后的明文
     */
    public static String decrypt(String entryStr ) throws Exception{
        String decryptStr = "";
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(entryStr.getBytes("UTF-8"));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(PRI_KEY);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        decryptStr = new String(cipher.doFinal(inputByte));
        return decryptStr;
    }

//    public static void main(String[] args)  {
//        try {
//            String jiami = entryInfo("15985641235");
//            System.out.println(jiami);
//            String jiemi = decrypt(jiami);
//            System.out.println(jiemi);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
}
